//
//  YXForgotTableView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/26.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXForgotTableView.h"
#import "YXAddPartnerTableView.h"
@interface YXForgotTableView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) NSArray *titleArr;
@property (nonatomic ,strong) NSArray *contentArr;
@property (nonatomic ,strong) UIView *footerView;
@property (nonatomic ,strong) UIButton *addPartnerBtn;
@property (nonatomic ,strong) YXAddPartnerTableViewcell *cell;
@end
@implementation YXForgotTableView


- (UIButton *)codeBtn {
    if (!_codeBtn) {
        _codeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _codeBtn.backgroundColor = color_globalColor;
        _codeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_codeBtn setTitle:@"获取验证码" forState:(UIControlStateNormal)];
        [_codeBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        _codeBtn.layer.masksToBounds = YES;
        _codeBtn.layer.cornerRadius = 8;
        [_codeBtn addTarget:self action:@selector(codeBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _codeBtn;
}

- (void)codeBtnAction:(UIButton *)sender {
    
    UITextField *tf1 = [self viewWithTag:2000];
    //获取验证码
    NSString *str = [self valiMobile:tf1.text];
    
    if(str) {
        [self showTostMessage:str];
    }else {
        [self GCDTime];
        self.codeBtnClickBlock(tf1.text);
    }

}

- (UIView *)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, kScreenHeight -  self.titleArr.count * 50-15))];
        [_footerView addSubview:self.addPartnerBtn];
    }
    return _footerView;
}

- (UIButton *)addPartnerBtn {
    if (!_addPartnerBtn) {
        _addPartnerBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _addPartnerBtn.frame = CGRectMake(20, 100, kScreenWidth - 40, 50);
        _addPartnerBtn.layer.masksToBounds = YES;
        _addPartnerBtn.layer.cornerRadius = 8.0f;
        _addPartnerBtn.backgroundColor = [UIColor py_colorWithHexString:@"#CBCCCD"];
        [_addPartnerBtn setTitle:@"确定" forState:(UIControlStateNormal)];
        _addPartnerBtn.userInteractionEnabled = NO;
        [_addPartnerBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [_addPartnerBtn addTarget:self action:@selector(addPartnerBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
    }
    return _addPartnerBtn;
    
}

- (void)addPartnerBtnAction:(UIButton *)sender {
    UITextField *tf1 = [self viewWithTag:2000];
    UITextField *tf2 = [self viewWithTag:2001];
    UITextField *tf3 = [self viewWithTag:2002];
    UITextField *tf4 = [self viewWithTag:2003];
    
    if (tf1.text.length== 0 || tf2.text.length== 0 || tf3.text.length== 0 ||tf3.text.length== 0) {
        [self showTostMessage:@"请填写完整信息！"];
    }else {
        NSDictionary *dict = @{@"account":tf1.text,@"smsCode":tf2.text,@"password":tf3.text,@"password2":tf4.text};
        self.addBtnClickBlock(dict);
    }

    
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style withTitleArr:(NSArray *)titleArr withContentArr:(NSArray *)contentArr {
    if (self = [super initWithFrame:frame style:style]) {
        self.backgroundColor = color_backColor;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.dataSource = self;
        self.delegate = self;
        self.titleArr = titleArr;
        self.contentArr = contentArr;
        self.tableFooterView = self.footerView;
        self.contentInset = UIEdgeInsetsMake(15, 0, 0, 0);
        [self registerClass:[YXAddPartnerTableViewcell class] forCellReuseIdentifier:@"YXAddPartnerTableViewcell"];

    }
    return self;
}

#pragma mark - UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"YXAddPartnerTableViewcell" forIndexPath:indexPath];
    self.cell.titleLab.text = self.titleArr[indexPath.row];
    self.cell.contentTF.placeholder = self.contentArr[indexPath.row];
    self.cell.contentTF.tag = indexPath.row + 2000;

    if (indexPath.row == 1) {
        [self.cell.contentView addSubview:self.codeBtn];
        __weak typeof (self.cell) weakSelf = self.cell;
        [_codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakSelf.mas_centerY);
            make.right.equalTo(weakSelf.mas_right).offset(-10);
            make.size.mas_equalTo(CGSizeMake(100, 36));
        }];
        
    }
    return self.cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.cell = [tableView cellForRowAtIndexPath:indexPath];
    [self.cell.contentTF becomeFirstResponder];
}

//制作timeButton的属性文本
- (NSMutableAttributedString *)makeTimeButtonAttributed:(NSInteger)time{
    NSMutableAttributedString *string3 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%li",time] attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    NSMutableAttributedString *string4 = [[NSMutableAttributedString alloc]initWithString:@"剩余" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    [string4 insertAttributedString:string3 atIndex:string4.length];
    return string4;
}
#pragma mark - time
- (void)GCDTime{
    
    __block NSInteger time = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        if (time < 1) {
            dispatch_source_cancel(timer);
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:@"重新发送" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
            dispatch_async(dispatch_get_main_queue(), ^{
                _codeBtn.userInteractionEnabled = YES;
                [_codeBtn setAttributedTitle:string forState:(UIControlStateNormal)];
                _codeBtn.backgroundColor = color_globalColor;
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                _codeBtn.userInteractionEnabled = NO;
                _codeBtn.backgroundColor = [UIColor py_colorWithHexString:@"#CBCCCD"];
                [_codeBtn setAttributedTitle:[self makeTimeButtonAttributed:time] forState:(UIControlStateNormal)];
                _addPartnerBtn.backgroundColor = color_globalColor;
                _addPartnerBtn.userInteractionEnabled = YES;
                
            });
            time --;
        }
    });
    dispatch_resume(timer);
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITextField *tf1 = [self viewWithTag:2000];
    UITextField *tf2 = [self viewWithTag:2001];
    UITextField *tf3 = [self viewWithTag:2002];
    UITextField *tf4 = [self viewWithTag:2003];
    [tf1 resignFirstResponder];
    [tf2 resignFirstResponder];
    [tf3 resignFirstResponder];
    [tf4 resignFirstResponder];

}


@end
